package learn.letcode.slidewindow;

/**
 * 时间滑动窗口算法  ----  无重复的最长的字符串
 */
public class SlideWindow {

    public  int longSub(String s){
        if (s==null || s.length() ==0){
            return 0;
        }
        if (s.length()==1){
            return 1;
        }
        if (s.length()==2){
            if (s.charAt(0)==s.charAt(1)){
                return 1;
            }
            return 2;
        }

        int max = 1;
        int left = 0;
        int length = s.length();

        while (left<length){
            for (int right=left+1;right<length;right++){
                if (!check(s,left,right)){
                    if (right-left>max){
                        max = right-left;
                    }
                    break;
                }

                if (right == length - 1){
                    if (right - left + 1 > max) {
                        max = right - left + 1;
                    }
                }

            }
            left++;
        }

        return max;

    }

    public boolean check(String s,int left,int right){
        for (int i=left;i<right;i++){
            if (s.charAt(right)==s.charAt(i)){
                return false;
            }
        }
        return true;
    }


    public static void main(String[] args) {
        String str = "abcdeabdsge";

        int i = new SlideWindow().longSub(str);

        System.out.println(i);

    }


}
